<template>
  <div>
    <el-card style="margin:20px 0">
      <el-table
        style="width: 100%"
        border
        :data="data"
      >
        <template slot="empty">
          <el-empty description="空" />
        </template>
        <el-table-column
          prop="prop"
          label="序号"
          width="80px"
          align="center"
          type="index"
        />
        <el-table-column
          prop="skuName"
          label="名称"
          width="width"
        />
        <el-table-column
          prop="skuDesc"
          label="描述"
          width="width"
        />
        <el-table-column
          prop="skuDefaultImg"
          label="默认图片"
          width="120px"
          align="center"
        >
          <template slot-scope="{row}">
            <div>
              <el-image
                style="width: 100px; height: 100px"
                :src="row.skuDefaultImg"
                fit="fit"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="weight"
          label="重量"
          width="100px"
        />
        <el-table-column
          prop="price"
          label="价格(元)"
          width="100px"
        />
        <el-table-column
          prop="prop"
          label="操作"
          width="250px"
        >
          <template slot-scope="{row}">
            <div>
              <el-button v-if="row.isSale===0" :disabled="$store.state.user.buttons.indexOf('btn.Sku.updown')===-1" type="warning" icon="el-icon-bottom" size="mini" title="下架" @click="onSale(row)" />
              <el-button v-else :disabled="$store.state.user.buttons.indexOf('btn.Sku.updown')===-1" type="success" icon="el-icon-top" size="mini" title="上架" @click="cancelSale(row)" />
              <el-button :disabled="$store.state.user.buttons.indexOf('btn.Sku.update')===-1" type="primary" icon="el-icon-edit" size="mini" title="编辑" @click="edit" />
              <el-button :disabled="$store.state.user.buttons.indexOf('btn.Sku.detail')===-1" type="info" icon="el-icon-warning-outline" size="mini" title="查看信息" @click="getSkuById(row.id)" />
              <el-button :disabled="$store.state.user.buttons.indexOf('btn.Sku.remove')===-1" type="danger" icon="el-icon-delete" size="mini" title="删除" />
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-size="5"
        :current-page="page"
        :page-sizes="[5,10,15]"
        layout="prev,pager,next,jumper,->,sizes,total"
        style="margin-top:20px;text-align:center"
        :total="total"
        @size-change="sizeChange"
        @current-change="currentChange"
      />
    </el-card>
    <el-drawer
      title="查看信息"
      :visible.sync="drawer"
      direction="rtl"
      size="50%"
    >
      <el-row type="flex" align="middle">
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ info.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ info.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ info.price }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <el-tag v-for="item in info.skuAttrValueList" :key="item.id" style="margin:0 10px 10px 0" type="success">{{ item.attrName +"-"+item.valueName }}</el-tag>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台图片</el-col>
        <el-col :span="16">
          <el-carousel indicator-position="outside">
            <el-carousel-item v-for="item in info.skuImageList" :key="item.id">
              <el-image
                style="width: 100%; height: 100%"
                :src="item.imgUrl"
                fit="contain"
              />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>

  </div>
</template>

<script>
export default {
  name: 'Sku',
  data() {
    return {
      page: 1,
      limit: 5,
      total: 0,
      data: [],
      info: {},
      drawer: false
    }
  },
  mounted() {
    this.productlist()
  },
  methods: {
    async productlist() {
      const res = await this.$api.sku.productlist(this.page, this.limit)
      this.total = res.data.total
      this.data = res.data.records
    },
    sizeChange(e) {
      this.limit = e
      this.productlist()
    },
    currentChange(e) {
      this.page = e
      this.productlist()
    },
    async cancelSale(e) {
      const res = await this.$api.sku.cancelSale(e.id)
      if (res.code === 200) {
        this.$message({ type: 'warning', message: '下架成功' })
        this.$set(e, 'isSale', 0)
      }
    },
    async  onSale(e) {
      const res = await this.$api.sku.onSale(e.id)
      if (res.code === 200) {
        this.$message({ type: 'success', message: '上架成功' })
        this.$set(e, 'isSale', 1)
      }
    },
    edit() {
      this.$message('正在开发中')
    },
    // 获取sku详情
    async getSkuById(e) {
      this.drawer = true
      const res = await this.$api.sku.getSkuById(e)
      if (res.code === 200) {
        this.info = res.data
        console.log(this.info)
      }
    }
  }
}
</script>

<style scoped>
/* 加上了scoped后还想影响到子组件中的样式的话需要使用/deep/ 深度选择器 ::v-deep sass中 原生中>>>*/
.el-row .el-col-5{
    font-size: 18px;
    text-align: right;
}

.el-col{
    margin:10px 10px;
}

.el-carousel__item h3 {
    color: #475669;
    font-size: 18px;
    opacity: 0.75;
    line-height: 300px;
    margin: 0;
  }

  /* .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }

  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  } */
  .el-carousel{
    width: 100%;
    height: 500px;
  }
</style>
